﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLFacade.entities;
using BLFacade.enums;
using BLFacade;

namespace ProjectUI
{
    public partial class MainForm : Form
    {

        public MainForm()
        {
            InitializeComponent();
            ShowLogin();
        }


        private void LogoutToolStripButton_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
            foreach (ToolStripItem item in toolStrip.Items)
            {
                item.Visible = true;
            }
            toolStrip.Enabled = false;
            ShowLogin();
        }

        private void ShowLogin()
        {
            FacadeHolder.UserFacade.CurrentPerson = null;
            LoginForm login = new LoginForm();
            login.MdiParent = this;
            login.Show();
        }

        private void ToolStripContacts_Click(object sender, EventArgs e)
        {
            ContactListForm contacts = new ContactListForm(((ICustomerContacts)FacadeHolder.UserFacade.CurrentPerson).Customer);
            contacts.MdiParent = this;
            contacts.Show();
        }

        private void ToolStripInfo_Click(object sender, EventArgs e)
        {
            Form info;
            if (FacadeHolder.UserFacade.IsEmployee()) info = new EmployeeForm((IEmployee)FacadeHolder.UserFacade.CurrentPerson);
            else 
            {
                info = new CustomerContactForm((ICustomerContacts)FacadeHolder.UserFacade.CurrentPerson);
                CustomerForm customer = new CustomerForm(((ICustomerContacts)FacadeHolder.UserFacade.CurrentPerson).Customer);
                customer.MdiParent = this;
                customer.Show();
            }
            info.MdiParent = this;
            info.Show();
        }

        private void ToolStripTasks_Click(object sender, EventArgs e)
        {
            TaskListForm tasks = new TaskListForm((IEmployee)FacadeHolder.UserFacade.CurrentPerson);
            tasks.MdiParent = this;
            tasks.Show();
        }

        internal void UserUpdate()
        {
            toolStrip.Enabled = (FacadeHolder.UserFacade.CurrentPerson != null);
            if (FacadeHolder.UserFacade.CurrentPerson != null)
            {
                if (!FacadeHolder.UserFacade.IsEmployee())
                {
                    ToolStripTasks.Visible = false;
                    ToolStripEmployees.Visible = false;
                    ToolStripCustomers.Visible = false;
                }
                else
                {
                    ToolStripContacts.Visible = false;
                    if (!FacadeHolder.UserFacade.IsHRManager()) ToolStripEmployees.Visible = false;
                    if (!FacadeHolder.UserFacade.IsAdministrator()) ToolStripCustomers.Visible = false;
                }
            }
        }

        private void ToolStripProjects_Click(object sender, EventArgs e)
        {
            ProjectListForm projects;
            if (FacadeHolder.UserFacade.IsEmployee())
            {
                if (FacadeHolder.UserFacade.IsAdministrator()) projects = new ProjectListForm();
                else projects = new ProjectListForm(((IEmployee)FacadeHolder.UserFacade.CurrentPerson));
            }
            else projects = new ProjectListForm(((ICustomerContacts)FacadeHolder.UserFacade.CurrentPerson).Customer);
            projects.MdiParent = this;
            projects.Show();
        }

        private void ToolStripEmployees_Click(object sender, EventArgs e)
        {
            EmployeeListForm employees = new EmployeeListForm();
            employees.MdiParent = this;
            employees.Show();
        }

        private void ToolStripCustomers_Click(object sender, EventArgs e)
        {
            CustomerListForm customers = new CustomerListForm();
            customers.MdiParent = this;
            customers.Show();
        }
    }
}
